import { createStore } from 'vuex'
import db from '@/db'
import system from './modules/system'
import folder from './modules/folder'
import filter from './modules/filter'
import sync from './modules/sync'

export default createStore({
  state: {
    alert: {
      color: 'error',
      show: false,
      message: '',
    },
  },
  modules: {
    system,
    folder,
    filter,
    sync,
  },
  mutations: {
    SET_ALERT(state, alert) {
      Object.assign(state.alert, alert)
    },
  },
  actions: {
    /**
     * 初始化数据
     * @param dispatch
     * @returns {Promise<void>}
     */
    async initializeStore({ dispatch }) {
      await dispatch('system/initializeSystem')
    },

    /**
     * 重置并清空数据
     * @param dispatch
     * @returns {Promise<void>}
     */
    async resetAndClear({ dispatch }) {
      await dispatch('system/resetAndClear')

      // 清空数据库 - 文件夹
      await db.folder.clear()

      // 清空数据库 - 会话
      await db.conversation.clear()

      // 清除filter
      await dispatch('filter/setFilter', {
        search: '',
      })
    },

    /**
     * 弹出提示框
     * @param commit
     * @param text
     */
    alert({ commit }, msg) {
      commit('SET_ALERT', {
        show: true,
        message: msg,
      })
    },
  },
})
